package net.wofly.sale.activity.validator;

import net.wofly.sale.activity.domain.Activity;
import net.wofly.sale.activity.domain.ActivityGift;
import net.wofly.sale.activity.util.ActivityContent;
import net.wofly.sale.activity.util.ActivityType;
import net.wofly.sale.order.util.OrderType;
import org.springframework.util.StringUtils;

import java.util.List;

public class ActivityValidator {
    
    /**
     * 换行符
     */
    private static String lineSeparator = System.getProperty("line.separator");
    
    public static StringBuffer validateActivityID(String activityID) {
        StringBuffer stringBuffer = new StringBuffer();
        if (StringUtils.isEmpty(activityID)) {
            stringBuffer.append("活动ID不能为空！").append(lineSeparator);
        }
        return stringBuffer;
    }
    
    public static StringBuffer validateCommodityChannelID(String commodityChannelID) {
        StringBuffer stringBuffer = new StringBuffer();
        if (StringUtils.isEmpty(commodityChannelID)) {
            stringBuffer.append("商品渠道ID不能为空！").append(lineSeparator);
        }
        return stringBuffer;
    }
    
    public static String validateAddActivityParams(Activity activity) {
        StringBuffer stringBuffer = new StringBuffer();
        
        if (activity == null) {
            stringBuffer.append("活动信息不能为空！").append(lineSeparator);
        } else {
            validateActivity(activity, stringBuffer);
            
        }
        
        return stringBuffer.toString();
    }
    
    public static String validateModifyActivityParams(Activity activity) {
        StringBuffer stringBuffer = new StringBuffer();
        
        if (activity == null) {
            stringBuffer.append("活动信息不能为空！").append(lineSeparator);
        } else {
            if (StringUtils.isEmpty(activity.getActivityID())) {
                stringBuffer.append("活动ID不能为空！").append(lineSeparator);
            }
            validateActivity(activity, stringBuffer);
        }
        return stringBuffer.toString();
    }
    
    public static String validateActivityTime(Activity activity) {
    		StringBuffer stringBuffer = new StringBuffer();
    		if (activity.getEndTime()<=activity.getStartTime()) {
            stringBuffer.append("活动开始时间不能大于活动结束时间！").append(lineSeparator);
        }
    		return stringBuffer.toString();
    }
    
    private static void validateActivity(Activity activity, StringBuffer stringBuffer) {
        if (StringUtils.isEmpty(activity.getActivityName())) {
            stringBuffer.append("活动名称(activityName)不能为空！").append(lineSeparator);
        }
        if (StringUtils.isEmpty(activity.getActivityType())) {
            stringBuffer.append("活动类型(activityType)不能为空！").append(lineSeparator);
        } else {
            if (activity.getOrderType() == null || (!OrderType.isValueCorrect(
                    activity.getOrderType()) && activity.getOrderType().intValue() != -1)) {
                stringBuffer.append("订单类型(orderType)不能为空！").append(lineSeparator);
            }
            if (ActivityType.单一商品数量累计_每满赠.getValue().equals(activity.getActivityType())) {
                if (activity.getNum() == null || activity.getNum() <= 0) {
                    stringBuffer.append("达到的数量(num)不能为空或者商品数量不正确！").append(lineSeparator);
                }
                if (StringUtils.isEmpty(activity.getCommodityChannelID())) {
                    stringBuffer.append("商品渠道ID不能为空！").append(lineSeparator);
                }
            } else if (ActivityType.单一订单中商品总价.getValue().equals(activity.getActivityType())){
                if (activity.getStartAmount() == null || activity.getStartAmount() <= 0) {
                    stringBuffer.append("达到开始总价(startAmount)不能为空或者总价数量不正确！").append(lineSeparator);
                }
                if (activity.getEndAmount() == null || activity.getEndAmount() <= 0) {
                    stringBuffer.append("达到终止总价(endAmount)不能为空或者总价数量不正确！").append(lineSeparator);
                }
            }
            
            if (activity.getStartTime() == null || activity.getEndTime() == null) {
                stringBuffer.append("活动的开始时间和结束时间不能为空！").append(lineSeparator);
            }
        }
    }
    
    public static String validateAddActivityGift(String activityID, ActivityGift activityGift) {
        StringBuffer stringBuffer = validateActivityID(activityID);
        if (null == activityGift) {
            stringBuffer.append("赠品(activityGift)不能为空！").append(lineSeparator);
        } else {
            validateActivityGift(stringBuffer, activityGift);
        }
        return stringBuffer.toString();
    }
    
    public static String validateAddActivityGifts(String activityID, List<ActivityGift> activityGift) {
        StringBuffer stringBuffer = validateActivityID(activityID);
        if (null == activityGift) {
            stringBuffer.append("赠品(activityGift)不能为空！").append(lineSeparator);
        } else {
            for (ActivityGift gift : activityGift) {
                validateActivityGift(stringBuffer, gift);
            }
        }
        return stringBuffer.toString();
    }
    
    private static void validateActivityGift(StringBuffer stringBuffer, ActivityGift activityGift) {
        if (activityGift.getActivityContent() == null || !ActivityContent.isValueCorrect(
                activityGift.getActivityContent())) {
            stringBuffer.append("activityContent不能为空或者activityContent 不正确！").append(lineSeparator);
        } else {
            if (activityGift.getActivityContent().intValue() == ActivityContent.赠品.getValue()) {
                if (StringUtils.isEmpty(activityGift.getCommodityTerminalID())) {
                    stringBuffer.append("终端商品ID不能为空！").append(lineSeparator);
                }
                if (activityGift.getNum() == null || activityGift.getNum() <= 0) {
                    stringBuffer.append("赠品数量不能为或者赠品数量不正确！").append(lineSeparator);
                }
            }
            if (activityGift.getActivityContent().intValue() == ActivityContent.打折.getValue()
                    && StringUtils.isEmpty(activityGift.getDiscountRate())) {
                stringBuffer.append("折扣率（discountRate）不能为空！").append(lineSeparator);
            }
        }
    }
}
